package hy.planegame.sprite

import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.RectF
import hy.planegame.R
import hy.planegame.utils.context
import hy.planegame.utils.paint
import hy.planegame.utils.screenHeight
import hy.planegame.utils.screenWidth

/**
 * Created time : 2017/8/31 15:41.
 *
 * @author HY
 */
class GameBackground : Sprite<GameBackground>() {
    private val bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.background1)
    private val bitmap2 = bitmap
    private val speed = 10

    private val width = screenWidth.toFloat()

    private val scale = bitmap.width / width

    private val height = bitmap.height / scale

    private val rectF1 = RectF()
    private val rectF2 = RectF()

    private var startY1 = screenHeight - height
    private var startY2 = startY1 - height

    override fun draw(canvas: Canvas?) {
        rectF1.set(0f, startY1, width, startY1 + height)
        rectF2.set(0f, startY2, width, startY2 + height)


        if (null != canvas) {
            canvas.drawBitmap(bitmap, null, rectF1, paint)
            canvas.drawBitmap(bitmap2, null, rectF2, paint)
        }
    }

    fun reset() {
        startY1 = 0f
        startY2 = height
        _onReset?.invoke()
    }

    private var _onReset: (() -> Unit)? = null

    fun onReset(callback: () -> Unit) {
        _onReset = callback
    }

    override fun logic() {
        startY1 += speed
        startY2 += speed

        if (startY1 >= screenHeight) {
            startY1 = startY2 - height
        }

        if (startY2 >= screenHeight) {
            startY2 = startY1 - height
        }
    }

    override fun destroy() {
        //销毁

    }
}